<% javascript 'backend/sortable' %>
<% javascript 'backend/flexible_content' %>

<div id="content-rows">
  <div class="cards">
    <% rows = model.content_rows.where(locale: locale) %>

    <% if rows.empty? %>
      <div class="card">
        <div class="card-header">
          <%= t 'b.flexible_content' %>
        </div>

        <div class="card-block">
          <%= t 'b.msg.flexible_content.explanation' %>
        </div>
      </div>
    <% end %>

    <% rows.decorate.each do |row| %>
      <div id="content-row-<%= row.id %>" class="card content-row" data-update-position="<%= update_position_backend_content_row_path(row) %>">
        <div class="card-header handle">
          <div class="btn-group" role="group">
            <%= link_to icon(:trash, t('b.msg.flexible_content.delete_row')), backend_content_row_path(row), class: 'btn btn-secondary', method: :delete, data: { confirm: t('b.msg.confirm') } %>
          </div>
        </div>

        <div class="row content-columns card-block">
          <% row.columns.each do |column| %>
            <% content = column.content.decorate %>

            <div class="content-column col-xs-<%= column.width_xs %> col-sm-<%= column.width_sm %> col-md-<%= column.width_md %> col-lg-<%= column.width_lg %> col-xl-<%= column.width_xl %>" data-update-position="<%= update_position_backend_content_row_column_path(row, column) %>">
              <div class="card handle">
                <div class="card-block">
                  <% Udongo.config.flexible_content.types.each do |content_type| %>
                    <%= render("backend/content/#{content_type}", object: content) if content.content_type_is?(content_type)  %>
                  <% end %>
                </div>

                <div class="card-footer">
                  <div class="btn-group" role="group">
                    <% edit_link = send("edit_backend_content_#{content.content_type}_path", content) %>
                    <%= link_to icon(:pencil_square_o), edit_link, class: 'btn btn-secondary btn-sm' %>
                    <%= link_to icon(:trash), backend_content_row_column_path(row, column), class: 'btn btn-secondary btn-sm', method: :delete, data: { confirm: t('b.msg.confirm') } %>
                    <%= link_to icon(:wrench), edit_backend_content_row_column_path(row, column), class: 'btn btn-secondary btn-sm' %>
                  </div>
                </div>
              </div>
            </div>
          <% end %>

          <% unless row.column_limit_reached? %>
            <% if row.columns.blank? %>
              <%= link_to icon(:plus, t('b.msg.flexible_content.add_column')), new_backend_content_row_column_path(row), class: 'btn btn-secondary btn-block' %>
            <% else %>
              <%= link_to icon(:plus, t('b.msg.flexible_content.add_column')), new_backend_content_row_column_path(row), class: 'btn btn-secondary hidden-xl-up full-width' %>
              <%= link_to icon(:plus), new_backend_content_row_column_path(row), class: 'btn btn-secondary hidden-lg-down block' %>
            <% end %>
          <% end %>
        </div>
      </div>
    <% end %>
  </div>

  <%= link_to icon(:plus, t('b.msg.flexible_content.add_row')), new_backend_content_row_path(klass: @model.class, id: @model.id, locale: locale), class: 'btn btn-secondary btn-block' %>
</div>
